package com.example.springboot01;


import lombok.ToString;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

public class DistributeSort {

    private static List<Data> d1 = new ArrayList<>();
    private static List<Data> d2 = new ArrayList<>();
    private static List<Data> d3 = new ArrayList<>();
    private static List<Data> d4 = new ArrayList<>();
    private static List<Data> d5 = new ArrayList<>();

    public static void main(String[] args) {
        final long t1 = System.currentTimeMillis();
        System.out.println(t1);
        final List<Data> data = page1(300, 100);
        for (Data datum : data) {
            System.out.println(datum);
        }
        final long t2 = System.currentTimeMillis();
        System.out.println(t2);
        System.out.println(t2 - t1);
    }

    private static List<Data> page1(int page, int size) {
        buildData();
        List<Data> all = new ArrayList<>();
        all.addAll(d1);
        all.addAll(d2);
        all.addAll(d3);
        all.addAll(d4);
        all.addAll(d5);
        all = all.stream().sorted(new Comparator<Data>() {
            @Override
            public int compare(Data o1, Data o2) {
                return o1.getTime().compareTo(o2.time);
            }
        }).collect(Collectors.toList());
        return (all.subList((page - 1) * size, page * size));
    }

//    private static List<Data> page2(int page, int size) {
//        buildData();
//        final int startIndex = (page - 1) * size;
//        final List<Data> data1 = d1.subList(0, startIndex + 1);
//        final List<Data> data2 = d2.subList(0, startIndex + 1);
//        final List<Data> data3 = d3.subList(0, startIndex + 1);
//        final List<Data> data4 = d4.subList(0, startIndex + 1);
//        final List<Data> data5 = d5.subList(0, startIndex + 1);
//
//        List<Data> all = new ArrayList<>();
//        all.addAll(data1);
//        all.addAll(data2);
//        all.addAll(data3);
//        all.addAll(data4);
//        all.addAll(data5);
//        all = all.stream().sorted(new Comparator<Data>() {
//            @Override
//            public int compare(Data o1, Data o2) {
//                return o1.getTime().compareTo(o2.time);
//            }
//        }).collect(Collectors.toList());
//        final Data data = all.get(startIndex);
//    }

    private static void buildData(){
        Random r = new Random();
        for (int i = 0; i < 1000000; i++) {
            d1.add(new Data(r.nextInt(50000), LocalDateTime.of(r.nextInt(2000) + 1, r.nextInt(12) + 1, r.nextInt(28) + 1, r.nextInt(24), r.nextInt(60), r.nextInt(60))));
            d2.add(new Data(r.nextInt(50000), LocalDateTime.of(r.nextInt(2000)+ 1, r.nextInt(12)+ 1, r.nextInt(28) + 1, r.nextInt(24), r.nextInt(60), r.nextInt(60))));
            d3.add(new Data(r.nextInt(50000), LocalDateTime.of(r.nextInt(2000)+ 1, r.nextInt(12)+ 1, r.nextInt(28) + 1, r.nextInt(24), r.nextInt(60), r.nextInt(60))));
            d4.add(new Data(r.nextInt(50000), LocalDateTime.of(r.nextInt(2000)+ 1, r.nextInt(12)+ 1, r.nextInt(28) + 1, r.nextInt(24), r.nextInt(60), r.nextInt(60))));
            d5.add(new Data(r.nextInt(50000), LocalDateTime.of(r.nextInt(2000)+ 1, r.nextInt(12)+ 1, r.nextInt(28) + 1, r.nextInt(24), r.nextInt(60), r.nextInt(60))));
        }
    }

    @ToString
    private static class Data {
        private Integer id;

        private LocalDateTime time;

        public Data(Integer id, LocalDateTime time) {
            this.id = id;
            this.time = time;
        }

        public Integer getId() {
            return id;
        }

        public void setId(Integer id) {
            this.id = id;
        }

        public LocalDateTime getTime() {
            return time;
        }

        public void setTime(LocalDateTime time) {
            this.time = time;
        }

    }
}
